package fr.afcepf.atod18.utilitaires.exemple;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
 
import org.springframework.dao.DataAccessException;
  
/**
 *
 * User Managed Bean
 *
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *
 */
@ManagedBean(name="userMB")
@RequestScoped
public class UserMB implements Serializable {
 
    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";
    private static final String ERROR   = "error";
 
    //Spring User Service is injected...
    @ManagedProperty(value="#{UserService}")
    IUserService userService;
 
    List<User> userList;
 
    private int id;
    private String name;
    private String surname;
 
    /**
     * Add User
     *
     * @return String - Response Message
     */
    public String addUser() {
        try {
            User user = new User();
            user.setId(getId());
            user.setName(getName());
            user.setSurname(getSurname());
            getUserService().addUser(user);
            return SUCCESS;
        } catch (DataAccessException e) {
            e.printStackTrace();
        }  
 
        return ERROR;
    }
 
    /**
     * Reset Fields
     *
     */
    public void reset() {
        this.setId(0);
        this.setName("");
        this.setSurname("");
    }
 
    /**
     * Get User List
     *
     * @return List - User List
     */
    public List<User> getUserList() {
        userList = new ArrayList<User>();
        userList.addAll(getUserService().getUsers());
        return userList;
    }
 
    /**
     * Get User Service
     *
     * @return IUserService - User Service
     */
    public IUserService getUserService() {
        return userService;
    }
 
    /**
     * Set User Service
     *
     * @param IUserService - User Service
     */
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
 
    /**
     * Set User List
     *
     * @param List - User List
     */
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
 
    /**
     * Get User Id
     *
     * @return int - User Id
     */
    public int getId() {
        return id;
    }
 
    /**
     * Set User Id
     *
     * @param int - User Id
     */
    public void setId(int id) {
        this.id = id;
    }
 
    /**
     * Get User Name
     *
     * @return String - User Name
     */
    public String getName() {
        return name;
    }
 
    /**
     * Set User Name
     *
     * @param String - User Name
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /**
     * Get User Surname
     *
     * @return String - User Surname
     */
    public String getSurname() {
        return surname;
    }
 
    /**
     * Set User Surname
     *
     * @param String - User Surname
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }
 
}